#!/usr/bin/env bash

help(){
    cat<<__EOF__
Usage: m firewall [OPTONS]

Description: Manage the macOS ApplicationFirewall

Options:
  --help                        Show this help message
  --status                      Show the current status of the firewall
  --enable                      Enable the firewall
  --disable                     Disable the firewall
  --list                        List applications handled by the firewall
  --add PATH_TO_APP             Add an application to the firewall
  --remove PATH_TO_APP          Remove an application from the firewall
  --blockall <enable|disable>   Block all connections (State 2)
__EOF__
}

FW_EXE="/usr/libexec/ApplicationFirewall/socketfilterfw"

case $1 in
    --help)
        help
        ;;
    --status)
        ${FW_EXE} --getglobalstate
        ;;
    --enable)
        sudo ${FW_EXE} --setglobalstate on
        ;;
    --disable)
        sudo ${FW_EXE} --setglobalstate off
        ;;
    --list)
        ${FW_EXE} --list
        ;;
    --add)
        [ -z "$2" ] && help && exit 1
        sudo ${FW_EXE} --add "${2}"
        ;;
    --remove)
        [ -z "$2" ] && help && exit 1
        sudo ${FW_EXE} --remove "${2}"
        ;;
    --blockall)
        [ "$2" != "enable" ] && [ "$2" != "disable" ] || [ -z "$2" ] && help && exit 1
        sudo ${FW_EXE} --setblockall $2
        ;;
    *)
        help
        ;;
esac
